/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.base.web.book;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.base.entity.catalog.BaseCatalog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.base.entity.book.BaseBook;
import com.thinkgem.jeesite.modules.base.service.book.BaseBookService;

/**
 * 基础信息_书籍信息生成Controller
 * @author 呆子
 * @version 2020-02-21
 */
@Controller
@RequestMapping(value = "${adminPath}/base/book/baseBook")
public class BaseBookController extends BaseController {

	@Autowired
	private BaseBookService baseBookService;
	
	@ModelAttribute
	public BaseBook get(@RequestParam(required=false) String id) {
		BaseBook entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baseBookService.get(id);
		}
		if (entity == null){
			entity = new BaseBook();
		}
		return entity;
	}


	@RequiresPermissions("base:book:baseBook:view")
	@RequestMapping(value = {"list", ""})
	public String list(BaseBook baseBook, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BaseBook> page = baseBookService.findPage(new Page<BaseBook>(request, response), baseBook); 
		model.addAttribute("page", page);
		return "modules/base/book/baseBookList";
	}
    @RequiresPermissions("base:book:baseBook:view")
    @RequestMapping(value = {"findBook", ""} )
    public String findBook(BaseBook baseBook, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<BaseBook> page = baseBookService.findBook(new Page<BaseBook>(request, response), baseBook);
        if(baseBook.getBookCatalogP()!=null){
            model.addAttribute("bookCatalogPid",baseBook.getBookCatalogP().getId());
        }
        if(baseBook.getBookCatalog()!=null){
            model.addAttribute("bookCatalogId",baseBook.getBookCatalog().getId());
        }
        model.addAttribute("page", page);
        return "modules/base/book/baseBookList";
    }

    //借阅管理_借阅添加/修改_弹窗页面_查询
    @RequiresPermissions("base:book:baseBook:view")
    @RequestMapping(value = {"selectBook", ""} )
    public String selectBook(BaseBook baseBook, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<BaseBook> page = baseBookService.selectBookPage(new Page<BaseBook>(request, response), baseBook);
        if(baseBook.getBookCatalogP()!=null){
            model.addAttribute("bookCatalogPid",baseBook.getBookCatalogP().getId());
        }
        if(baseBook.getBookCatalog()!=null){
            model.addAttribute("bookCatalogId",baseBook.getBookCatalog().getId());
        }
        model.addAttribute("page", page);
        return "modules/base/book/selectBookList";
    }

    //基础管理_书籍管理_主页面列表_查询
//    @RequiresPermissions("base:book:baseBook:view")
//    @RequestMapping(value = {"indexFindBook", ""} )
//    public String indexFindBook(BaseBook baseBook, HttpServletRequest request, HttpServletResponse response, Model model) {
//        Page<BaseBook> page = baseBookService.selectBookPage(new Page<BaseBook>(request, response), baseBook);
//        if(baseBook.getBookCatalogP()!=null){
//            model.addAttribute("bookCatalogPid",baseBook.getBookCatalogP().getId());
//        }
//        if(baseBook.getBookCatalog()!=null){
//            model.addAttribute("bookCatalogId",baseBook.getBookCatalog().getId());
//        }
//        model.addAttribute("page", page);
//        return "modules/base/book/baseBookList";
//    }

	@RequiresPermissions("base:book:baseBook:view")
	@RequestMapping(value = "form")
	public String form(BaseBook baseBook, Model model) {
		model.addAttribute("baseBook", baseBook);
		return "modules/base/book/baseBookForm";
	}

	@RequiresPermissions("base:book:baseBook:edit")
	@RequestMapping(value = "save")
	public String save(BaseBook baseBook, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, baseBook)){
			return form(baseBook, model);
		}
		baseBookService.save(baseBook);
		addMessage(redirectAttributes, "保存书籍信息成功");
		return "redirect:"+Global.getAdminPath()+"/base/book/baseBook/list?repage";
	}
	
	@RequiresPermissions("base:book:baseBook:edit")
	@RequestMapping(value = "delete")
	public String delete(BaseBook baseBook, RedirectAttributes redirectAttributes) {
		baseBookService.delete(baseBook);
		addMessage(redirectAttributes, "删除书籍信息成功");
		return "redirect:"+Global.getAdminPath()+"/base/book/baseBook/list?repage";
	}

    @RequestMapping(value = "baseBookIndex")
    public String baseCatalogIndex(BaseCatalog baseCatalog, Model model) {
        return "modules/base/book/baseBookIndex";
    }

    @RequestMapping(value = "selectBookIndex")
    public String selectBookIndex(BaseCatalog baseCatalog, Model model) {
        return "modules/base/book/selectBookIndex";
    }
}